/*
 * Inode.cpp
 *
 *  Created on: Mar 25, 2010
 *      Author: Lukasz Spintzyk
 */
#include "ph.h"
#include "Inode.h"

Inode::Inode()
{
	memset(this, 0, sizeof(Inode));
	assert(sizeof(Inode) == 128);
}

Inode::~Inode()
{
}


uint32_t Inode::size()
{
	return sizeof(Inode);
}

void Inode::read(void* buffer)
{
	memcpy((void*)this, buffer, Inode::size());
	assert(testInode());
}

void Inode::write(void*buffer) const
{
	memcpy(buffer, (void*)this, Inode::size());
	assert(testInode());
}

bool Inode::testInode() const
{
	// add tests here
	return true;
}

bool Inode::isFile()
{
	return d_mode & S_IFREG;
}

bool Inode::isDir()
{
	return d_mode & S_IFDIR;
}

bool Inode::isSymlink()
{
	return d_mode & S_IFLNK;
}

bool Inode::isChrDev()
{
	return d_mode & S_IFCHR;
}

bool Inode::isBlkDev()
{
	return d_mode & S_IFBLK;
}

bool Inode::isFIFO()
{
	return d_mode & S_IFIFO;
}

bool Inode::isSocket()
{
	return d_mode & S_IFSOCK;
}

InodePtr Inode::newInstance()
{
	return InodePtr(new Inode());
}

void Inode::initLinkInode(struct fuse_context* context)
{
	d_atime = time(NULL);
	d_ctime = time(NULL);
	d_dtime = time(NULL);
	d_mtime = time(NULL);
	d_blocks = 0;
	d_gid = context->gid;
	d_uid = context->uid;
	d_linksCount = 1;
	d_mode = 0 | S_IFLNK |
				(S_IRUSR|S_IRGRP|S_IROTH) |
				(S_IWUSR|S_IWGRP|S_IWOTH) |
				(S_IXUSR|S_IXGRP|S_IXOTH);
}


void Inode::initDirInode(struct fuse_context* context, mode_t mode)
{
	mode |= S_IFDIR;
	d_mode = mode;
	d_ctime = time(NULL);
	d_blocks = 0;
	d_size = 0;
	d_dtime = 0;
	d_flags = 0;
	d_gid = context->gid;
	d_uid = context->uid;
	d_linksCount = 1;
	d_mtime = d_ctime;
}


